-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
classValidatorToJsonSchema - Get JSON Schema for a specific class #48
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution! This will totally be a useful addition. Left a few comments.
Also, does this work with nested validation? https://github.com/typestack/class-validator#validating-nested-objects. I have a feeling it might not because of the target metadata filtering in https://github.com/epiphone/class-validator-jsonschema/pull/48/files#diff-a2a171449d862fe29692ce031981047d7ab755ae7f84c707aef80701b3ea0c80R79.
@@ -12,7 +12,7 @@ module.exports = { | |||
moduleDirectories: ['node_modules', 'src'], | |||
moduleFileExtensions: ['ts', 'js', 'json'], | |||
roots: ['<rootDir>/__tests__'], | |||
testPathIgnorePatterns: ['/node_modules/'], | |||
testPathIgnorePatterns: ['/node_modules/', '/__tests__/classes'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary?
export class User { | ||
@MinLength(5) | ||
@IsString() | ||
name: string; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to use namespaces in this file to import two classes with the same name? Might be clearer than declaring one here and another in a separate file.
I mean something like
namespace Foo { export class User { } }
namespace Bar { export class User { } }
const targetMetadatas = _(metadatas) | ||
.filter(metadata => { | ||
return metadata.target === target | ||
}).value(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use the native Array.filter
here instead of lodash
/** | ||
* Convert class-validator class into JSON Schema definition. | ||
*/ | ||
export type ClassType<T> = new (...args: any[]) => T; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export type ClassType<T> = new (...args: any[]) => T; | |
export type ClassType = new (...args: any[]) => any; |
We can omit the unused generic here and below I think.
export class User { | ||
@MinLength(5) | ||
@IsString() | ||
name: string; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also test that if the two User classes both have a property with the same name but different class-validator decorators, only the desired decorators get applied in the resulting schema?
Hello.
First of all, thank you for amazing package! It is very useful, but in my case I needed ability to extract JSON Schema for a specific class (with duplicate names).
The problem with
validationMetadatasToSchemas
is that it merges schemas with the same names.So if I have 1 User class with
name
property and another withfirstName
, JSON Schema will include both (obviously I could use unique names, but for current project I cannot avoid duplicates).As a workaround I created a separate function which solves the issue for me and might be useful for someone else as well.